package GDD;

import parser.PConstants;
public class GCondition {
	public String operator;
	public short opType;
	public String leftOP;
	public String rightOP;
	
	GCondition(String c){
		if( c.indexOf(">=")>0){
			operator = new String(">=");
			opType = PConstants.NODE_LARGER_EQUAL;
		}
		else if( c.indexOf("<=")>0){
			operator = new String("<=");
			opType = PConstants.NODE_LESS_EQUAL;
		}
		else if( c.indexOf('=')>0){
			operator = new String("=");
			opType = PConstants.NODE_EQUAL;
		}
		else if( c.indexOf('<')>0){
			operator = new String("<");
			opType = PConstants.NODE_LESS;
		}
		else if( c.indexOf('>')>0){
			operator = new String(">");
			opType = PConstants.NODE_LAEGER;
		}
		int pos = c.indexOf(operator);
		//leftOP
		leftOP = new String(c.substring(0,pos));
		//remove blanks at the end
		while(leftOP.endsWith(" "))
		{
			leftOP = leftOP.substring(0,leftOP.length()-1);
		}
		//rightOP
		if(operator.startsWith(">=") || operator.startsWith("<="))
			rightOP = new String (c.substring(pos+2));
		else rightOP = new String (c.substring(pos+1));
		//remove blanks at the start
		while(rightOP.startsWith(" "))
		{
			rightOP = rightOP.substring(1);
		}
	}
	public boolean isBelong(String value){
		switch(opType){
		case PConstants.NODE_EQUAL:
			if(rightOP.compareTo(value) == 0)
				return true;
			else
				return false;
		case PConstants.NODE_LAEGER:
			if(Integer.parseInt(value)>Integer.parseInt(rightOP))
				return true;
			else
				return false;
		case PConstants.NODE_LARGER_EQUAL:
			if(Integer.parseInt(value)>=Integer.parseInt(rightOP))
				return true;
			else
				return false;
		case PConstants.NODE_LESS:
			if(Integer.parseInt(value)<Integer.parseInt(rightOP))
				return true;
			else
				return false;
		case PConstants.NODE_LESS_EQUAL:
			if(Integer.parseInt(value)<=Integer.parseInt(rightOP))
				return true;
			else
				return false;
		}
		return false;
	}
	public boolean isBelong(String value, int type){
		switch(type){
		case PConstants.NODE_EQUAL:
			return isBelong(value);
		case PConstants.NODE_LAEGER:
			//
		case PConstants.NODE_LARGER_EQUAL:
			//
		case PConstants.NODE_LESS:
			//
		case PConstants.NODE_LESS_EQUAL:
			//
		}
		return false;
	}
}
